// pages/createcard/goods/detail.js
const API = require('../../api/common.js');
import {
  getpdddetail,
  getpddgenerate,
  getpddSearch

} from "../../api/common";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    posts: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id,
      share:options.share
    })
    this.getDateil({
      goods_sign: options.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  getDateil: function(args) {
    getpdddetail(args).then(res => {
      console.log(res);
      this.setData({
        goodsInfo: res.goods_detail_response.goods_details[0],
      })
      this.getSearchPosts({
        keyword: res.goods_detail_response.goods_details[0].category_name,
        page: 1
      })
    })
  },

  getSearchPosts: function(args) {
    getpddSearch(args).then(res => {
        if (res.length < 10) {
          this.setData({
            isLastPage: true,
            loadtext: '到底啦',
            showloadmore: false
          })
        }
        console.log(res.goods_search_response.goods_list)
        let args = {}
        if (this.data.isPull) {
          args.posts = [].concat(this.data.posts, res.goods_search_response.goods_list)
          args.page = this.data.page + 1
        } else if (this.data.isBottom) {
          args.posts = [].concat(this.data.posts, res.goods_search_response.goods_list)
          args.page = this.data.page + 1
        } else {
          args.posts = [].concat(this.data.posts, res.goods_search_response.goods_list)
          args.page = this.data.page + 1
        }
        if (this.data.isPull) {
          wx.stopPullDownRefresh()
        }
        if (args.posts != []) {
          args.isList = true
        }
        //wx.setNavigationBarTitle({title: res.title.rendered})
        args.isPull = false
        this.setData(args)
      })
      .catch(err => {
        this.setData({
          isPull: false,
          isLastPage: true,
          loadtext: '到底啦',
        })
        wx.stopPullDownRefresh()
      })
  },


  creatlist: function() {
    let id = this.data.id;
    wx.navigateTo({
      url: '/pages/shopshare/detail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  goodsDetails: function (e) {
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/shop/pddetail?id=' + id,
    })
  },

  gotobuy: function() {
    wx.showLoading({
      title: '正在获取链接',
    })
    let id = this.data.id;
    getpddgenerate({
      goods_sign: id
    }).then(res => {
      console.log(res);
      wx.hideLoading();
      wx.navigateToMiniProgram({
        appId: res.goods_promotion_url_generate_response.goods_promotion_url_list[0].we_app_info.app_id,
        path: res.goods_promotion_url_generate_response.goods_promotion_url_list[0].we_app_info.page_path
      })
    })
  },

  onShareAppMessage(){
    return{
      title: this.data.goodsInfo.goods_name,
      image: this.data.goodsInfo.goods_image_url,
      path: '/pages/createcard/goods/detail?share=true&id=' + this.data.id,
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})